package com.ll.microuser.user.controller;

import cn.hutool.Hutool;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ll.common.core.annotation.NoLogin;
import com.ll.common.core.common.R;
import com.ll.common.core.entity.User;
import com.ll.common.core.ex.BusinessException;
import com.ll.common.core.utils.UserUtils;
import com.ll.common.minio.utils.MinioUtils;
import com.ll.microuser.user.mapper.UserMapper;
import com.ll.microuser.user.pojo.DTO.LoginDTO;
import com.ll.microuser.user.pojo.DTO.UserDTO;
import com.ll.microuser.user.pojo.VO.MyVO;
import com.ll.microuser.user.sercvice.IUserService;
import com.ll.microuser.user.sercvice.Impl.UserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MinioUtils minioUtils;


    @NoLogin
    @RequestMapping("/login")
    public R login(@RequestBody LoginDTO loginDTO){
        String token = userService.login(loginDTO);
        return R.ok(token);
    }

    @NoLogin
    @RequestMapping("/register")
    public R register(@RequestBody UserDTO userDTO){
        log.info("用户注册开始",userDTO);
        userService.register(userDTO);
        return R.ok();
    }
    @NoLogin
    @PatchMapping("/PatchPWD")
    public R PatchPWD(@RequestBody UserDTO userDTO){
        log.info("用户修改密码开始",userDTO);
        User user = userMapper.selectOne(Wrappers.lambdaQuery(User.class).eq(User::getPhone,userDTO.getPhone()));
        user.setPassword(userDTO.getPassword());
        userService.updateById(user);
        return R.ok();
    }

    @NoLogin
    @RequestMapping("/GetPhone")
    public R getphone(@RequestParam(value = "phone", required = true) String phone){
        System.out.println(phone);
        LambdaQueryWrapper<User> eq = Wrappers.lambdaQuery(User.class);
        boolean exists = userMapper.exists(eq.eq(User::getPhone, phone));
        if(exists) {
            return R.fail("手机号已存在");
        }
        return R.ok(phone);
    }

    @NoLogin
    @RequestMapping("/getCode")
    public R getCode(String  phone){
        String code = userService.sendCode(phone);
        return R.ok(code);
    }
    /**
     * 查询用户信息
     */
    @GetMapping("/my")
    public R my(){
        MyVO myVO = userService.getInfo();
        return R.ok(myVO);
    }

    @PostMapping("/upload/head")
    public R uploadHead(@RequestParam("file") MultipartFile file) throws IOException {
        String filename = RandomUtil.randomString(10);
        String[] split = file.getOriginalFilename().split("\\.");
        String suffix = split[split.length - 1];
        String avatar = minioUtils.upload(filename+suffix, file.getInputStream());
        User user = new User();
        Integer uId = UserUtils.getUId();
        user.setId(uId);
        user.setAvatar(avatar);
        userService.updateById(user);
        return R.ok(avatar);
    }

    @GetMapping("/info")
    public R info(){
        Integer uId = UserUtils.getUId();
        User user = userMapper.selectById(uId);
        return R.ok(user);
    }

    @PostMapping("/update")
    public R update(@RequestBody User user){
        Integer uId = UserUtils.getUId();
        user.setId(uId);
        userService.updateById(user);
        return R.ok();
    }
}
